<template>
  <view class="message">
    <sickTitleCom></sickTitleCom>
    <view v-for="(health, indexPar) in ajaxResMessage" :key="indexPar" class="box">
      <view v-if="health.nameKey" class="icon"><img :src="notKnowIcon" /></view>
      <view v-else class="icon"><img :src="waringIcon" /></view>
      <view class="name"
        >{{ health.title | formatTitle }}<span v-if="health.nameKey">待定</span
        ><span v-else>{{ health.title !== '慢病' && health.title !== '家族史' ? '异常' : '' }}</span></view
      >
      <view :class="['content', health.nameKey ? 'textAlignCen' : null]">
        <view v-for="(item, index) in health.healthGuidances" :key="index">{{ item }}</view>
      </view>
      <!-- <view class="iKnow"><button type="default">继续</button></view> -->
      <!-- 下一题上一题按钮 -->
      <nextProBtnCom
        :tipsCom="tipsCom"
        @iKnowBtn="iKnowBtn"
        @nextBtn="nextBtn"
        @proBtn="proBtn"
        @submitPage="submitPage"
      ></nextProBtnCom>
    </view>
  </view>
</template>

<script>
import waring from '@/static/icon/waring.png';
import notKnow from '@/static/icon/notKnow.png';
import sickTitleCom from './sickTitleCom';
import nextProBtnCom from '@/components/nextProButton';
import { addPhysicalExamination } from '@/api/assessQuestion';
// import { debounce } from '@/utils/optimize';
import { mapGetters } from 'vuex';
import { registerSource } from '@/common/baseUrl.js';
export default {
  components: {
    sickTitleCom,
    nextProBtnCom
  },
  filters: {
    formatTitle(value) {
      if (value === '脉压') {
        return '脉压差';
      } else if (value === '慢病') {
        return '有慢病';
      } else if (value === '家族史') {
        return '有家族史';
      } else {
        return value;
      }
    }
  },
  props: {
    // 是否展示搜索按钮
    uName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      mainData: null,
      waringIcon: waring,
      notKnowIcon: notKnow,
      tipsCom: true
    };
  },
  computed: {
    ...mapGetters(['active', 'ajaxResMessage', 'ownSickList', 'assessForm', 'familySickList', 'userSex']),
    ownSickData() {
      return this.ownSickList;
    },
    familySickData() {
      return this.familySickList;
    },
    assessPatientId() {
      return this.$store.getters.assessPatientId || wx.getStorageSync('assessPatientId');
    },
    loginPatientId() {
      return wx.getStorageSync('patientid') || '';
    }
  },
  methods: {
    iKnowBtn() {
      /* eslint-disable */
      // 疾病直接提交接口
      // console.log('this.active:',this.active)
      let curActive = 0;
      // if (this.userSex && this.userSex === '男' && this.active === 6) {
      //   curActive = this.active + 2;
      // } else {
      //   curActive = this.active + 1;
      // }
      // 这里不做计算，而是直接赋值，避免用户频繁点击导致计算错误，出现跳页的情况
      if (this.active === 0) {
        curActive = 1;
      } else if (this.active === 1) {
        curActive = 2;
      } else if (this.active === 2) {
        curActive = 3;
      } else if (this.active === 3) {
        curActive = 4;
      }

      this.$store.commit('assessQuestion/SET_AJAXRES', false);
      this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
      wx.pageScrollTo({
        scrollTop: 0
      });
    },
    // iKnowBtn() {
    //   // 疾病直接提交接口
    //   let curActive = this.$store.getters.active + 1;
    //   this.$store.commit('assessQuestion/SET_AJAXRES', false);
    //   this.$store.commit('assessQuestion/SET_ACTIVE', curActive);
    // },
    async submitPage() {
      console.log(111, this.ownSickData);
      this.assessForm.patientChronicDiseases = this.ownSickData.map((item) => {
        return { icd10: item.diseaseId };
      });
      this.assessForm.patientFamilyDiseases = this.familySickData.map((item) => {
        return { icd10: item.diseaseId, relation: item.relation };
      });
      // 个人其它疾病逻辑处理
      let { patientChronicDiseases } = this.assessForm;
      console.log(patientChronicDiseases);
      const otherIndex = patientChronicDiseases.findIndex((item) => item.icd10 === 'otherSickOwn');
      if (otherIndex > -1 && patientChronicDiseases.length === 1) {
        // 只选了其它疾病
        this.assessForm.isSick = 1;
        patientChronicDiseases = [];
      } else if (otherIndex === -1 && patientChronicDiseases.length === 0) {
        // 选了未患病
        this.assessForm.isSick = 0;
        patientChronicDiseases = [];
      }
      if (otherIndex > -1) {
        // 选了其它疾病和其它具体疾病，删除其它疾病传值
        patientChronicDiseases.splice(otherIndex, 1);
      }
      this.assessForm.patientChronicDiseases = patientChronicDiseases;
      this.assessForm.source = registerSource;
      this.assessForm.businessSource = 2;
      uni.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 15000,
        mask: true
      });
      if (this.assessForm.height - 0 >= 50) {
        this.assessForm.height = this.assessForm.height / 100;
      }
      this.assessForm.createBy = this.loginPatientId;
      addPhysicalExamination(this.assessPatientId, this.assessForm).then((res) => {
        if (res.code === 0) {
          this.$wxPromise.navigateTo({
            url: '/pagesB/assessSick/assessResultPage?uName=' + this.uName
          });
        } else {
          uni.showToast({ title: res.message, icon: 'none' });
        }
        setTimeout(function () {
          uni.hideToast();
        }, 1800);
      });
    }
  }
};
</script>
<style lang="scss">
.message {
  width: 100%;
  height: 100%;
  background: #fff;
  .box {
    text-align: center;
    padding: 44rpx 24rpx;
    .icon {
      img {
        width: 100rpx;
        height: 100rpx;
      }
    }
    .name {
      font-size: 42rpx;
      padding: 24rpx;
    }
    .content {
      font-size: 32rpx;
      text-align: left;
      color: rgba(0, 0, 0, 0.7);
      text-align: center;
    }
    .textAlignCen {
      text-align: center;
    }
  }
}
</style>
